package com.andong.controller;

import com.andong.annotations.AccessControl;
import com.andong.domain.entity.Vehicle;
import com.andong.domain.pojo.PageResult;
import com.andong.domain.pojo.Result;
import com.andong.service.VehicleService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.time.LocalDate;

import static com.andong.utils.SystemConstant.*;

/**
 * 车辆信息表(Vehicle)表控制层
 *
 * @author makejava
 * @since 2024-01-03 18:20:10
 */
@RestController
@RequestMapping("/vehicle")
public class VehicleController {
    @Resource
    private VehicleService vehicleService;

    @PostMapping("/add")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "车辆管理")
    public Result add(@RequestBody Vehicle vehicle) {
        return vehicleService.add(vehicle);
    }

    @DeleteMapping("/{id}")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "车辆管理")
    public Result deleteById(@PathVariable Long id) {
        return vehicleService.delete(id);
    }

    @PutMapping
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "车辆管理")
    public Result update(@RequestBody Vehicle vehicle) {
        return vehicleService.modify(vehicle);
    }

    @GetMapping("/list")
    @AccessControl(roles = {STUDENT, ADMIN, SUPER_ADMIN}, permission = "车辆管理")
    public Result list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       Integer status, Integer type, String plateNumber,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        PageResult pageResult = vehicleService.queryList(page, pageSize, status, type, plateNumber, begin, end);
        return Result.success(pageResult);
    }

    @PutMapping("/status")
    @AccessControl(roles = {ADMIN, SUPER_ADMIN}, permission = "车辆管理")
    public Result updateStatus(Long id, Integer status) {
        return vehicleService.updateStatus(id, status);
    }
}

